import 'package:flutter/material.dart';

class See extends StatefulWidget {
  const See({Key? key}) : super(key: key);

  @override
  _SeeState createState() => _SeeState();
}

//1.同时mixin SingleTickerProviderStateMixin
class _SeeState extends State<See> with SingleTickerProviderStateMixin{
  late TabController _tabController;

  //2.自定义TabBar的好处是可以监听一些事件，进行额外操作
  @override
  void initState() {//生命周期函数
    // TODO: implement initState
    super.initState();
    _tabController = new TabController(
      vsync: this,
      length: 7,
    );
    //tabController监听的方法测试使用
    /*_tabController.addListener(() {
      print(_tabController.index);
    });*/
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          children: <Widget>[
            Expanded(
                child: TabBar(
                  isScrollable: true,
                  controller: this._tabController,//3.注意这儿要设置controller
                  tabs: <Widget>[
                    Tab(text: '老人安全'),
                    Tab(text: '儿童安全'),
                    Tab(text: '两性'),
                    Tab(text: '卫生'),
                    Tab(text: '公共'),
                    Tab(text: '广场'),
                    Tab(text: '居家安全'),
                  ],
                )
            ),
          ],
        ),
      ),
      body: TabBarView(
        controller: this._tabController,//4.注意这儿要设置controller
        children: <Widget>[
          Text('1'),
          Text('2'),
          Text('3'),
          Text('4'),
          Text('5'),
          Text('6'),
          Text('7'),
        ],
      ),
    );
  }
}
